/*******************************************************************************
 * Copyright (c) 2010 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package com.googlecode.ssdutils.beanconverter.example.presentation.table;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.googlecode.ssdutils.beanconverter.example.business.api.TodoListBusinessService;

/**
 * @author Sylvain Sicard
 * 
 */
public class ItemTableModel extends AbstractTableModel {

	protected TodoListBusinessService todoListBS;

	protected JTable listTable;

	public void setListTable(JTable listTable) {
		this.listTable = listTable;
	}

	public void setTodoListBS(TodoListBusinessService todoListBS) {
		this.todoListBS = todoListBS;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return true;
	}

	public int getColumnCount() {
		return 1;
	}

	@Override
	public String getColumnName(int column) {
		return "Items";
	}

	public int getRowCount() {
		return todoListBS.itemCount(listTable.getSelectedRow());
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		todoListBS.createItem(rowIndex, (String) value);
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		return todoListBS.getItemById(listTable.getSelectedRow(), rowIndex)
				.getLabel();
	}
}
